package com.csj.cloud.order.filter;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @Author:ChengshaoJie
 * @Description:
 * @Date:Created in 上午 11:31 2019/12/9 0009
 */
public class FilBloomFilter{

    private static int capacity = 1000000;
    private static int key = 999998;
    private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), capacity);
    private static HashMap hashSet = new HashMap<>();
    static {
        for (int i = 0; i < capacity; i++) {
            bloomFilter.put(i);
            hashSet.put(i,i);
        }
    }

    public static void main(String[] args) {
        long time1 = System.nanoTime();
        if (bloomFilter.mightContain(key)){
            System.out.println("在布隆过滤器中成功过滤到"+key);
        }
        long time2 = System.nanoTime();
        System.out.println("布隆断时间"+(time2-time1)+"纳秒");
        if (hashSet.containsKey(key)){
            System.out.println("在hashSet中过滤到"+key);
        }
        long time3 = System.nanoTime();
        System.out.println("hashSet时间"+(time3-time2)+"纳秒");
        int sum = 0;
        for (int i = capacity+20000;i<capacity+30000;i++){
            if (bloomFilter.mightContain(i)){
                sum++;
            }
        }
        System.out.println("错判率为:" + sum);
    }
}
